<?php
	class Transport extends CI_Controller {
	   
       function __construct() {
	       parent::__construct();
           $user = $this->session->userdata('user');
           if (!$user) redirect('main/alogin');
           $this->load->model('transport_db','Transport');
           $this->load->model('Dataset_db');
	   }
       
       function addarea() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; 
            }
            //print_object($_POST); exit;
            $data = array();

            if($_POST['btnAddArea']) {
                //print_object($_POST); exit;
                $pdata = array();
                $fields = array(
                		"area_name",
                        "area_code"			
                		);
                foreach ($fields as $field) {
                	if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field, true);
                }
                
                $this->form_validation->set_rules('area_name', 'Area Name', 'trim|required|max_length[50]|xss_clean');
                $this->form_validation->set_rules('area_code', 'Area Code', 'trim|xss_clean');
                
                $this->form_validation->run();
                $data["errors"] = $this->form_validation->error_array();
                
                
                if(!$this->Transport->is_areaname_unique($this->input->post("area_name",true))) {
                    if(is_array($data['errors']))
                        array_push($data["errors"],"Area already exist.");
                    else
                        $data["errors"] = array("Area already exist.");
                }
                
                $data["postdata"] = $pdata;
                   
                if (count($data["errors"])>0) {
                    $data['status'] == "F";
                } else {
                    //print_object($pdata); exit;
                    $user = $this->session->userdata('user');
                    $pdata["createdby"] = $user['userid'];
                    $pdata["createdon"] = unix_to_human(app_time(), true);
                    
                    
                    $save_result = $this->Transport->save_area($pdata);
                    if($save_result['result'] == 'S') {
						$data['id'] = $save_result['insert_id'];
						$data['label'] = $pdata['area_name'];
                        $data["successes"] = array('Area added successfully.');
                        $data["postdata"] = "";
						$data['status'] = "S";
                    } else {
						$data['status'] = "F";
                        $data["errors"] = array($save_result['error_message']);   
                    }
                    //print_object($data); exit;   
                }
				
				if($_POST['req_type'] == 'json') {
					echo json_encode($data); exit;
				}
				
            }
                                
            $navResult = $this->Dataset_db->buildNav();
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $this->load->view('addarea',$data);
        }
        
        function editarea() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; 
            }
            //print_object($_POST); exit;
            $data = array();
            
            $area_id = $this->input->post("area_id", true);
            
            if( isset( $area_id ) ) {
                if($_POST['btnUpdateArea']) {
                    //print_object($_POST); exit;
                    $pdata = array();
                    $fields = array(
                    		"area_name",
                            "area_code"		
                    		);                    
                    
                    $this->form_validation->set_rules('area_name', 'Area Name', 'trim|required|min_length[3]|max_length[100]|xss_clean');
                    $this->form_validation->set_rules('area_code', 'Area Code', 'trim|min_length[3]|max_length[10]|xss_clean');
                            
                    foreach ($fields as $field) {
                    	if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field, true);
                    }
                                    
                    $this->form_validation->run();
                    $data["errors"] = $this->form_validation->error_array();
                    
                    if(!$this->Transport->is_areaname_unique($this->input->post("area_name",true),$area_id)) {
                        if(is_array($data['errors']))
                            array_push($data["errors"],"Area already exist.");
                        else
                            $data["errors"] = array("Area already exist.");
                    }
                    
                    $data["postdata"] = $pdata;
                       
                    if (count($data["errors"])>0) {
                        $data['status'] == "F";
                    } else {
                        //print_object($pdata); exit;
                        
                        $save_result = $this->Transport->save_area($pdata, $area_id);
                        if($save_result['result'] == 'S') {
                            $data["successes"] = array('Area updated successfully.');
                            $data["postdata"] = "";
                            $ar_result = $this->Transport->get_area(array('area_id'=> $area_id));
                            if(count($ar_result) == 0) {
                                $data['errors'] = array("Could not find the selected area");
                            } else {
                                foreach($ar_result[0] as $key => $area_item) {
                                    $data[$key] = $area_item;
                                }
                            }
                        } else {
                            $data["errors"] = array($save_result['error_message']);  
                            foreach($pdata as $key => $st_item) {
                                $data[$key] = $st_item;
                            } 
                        }   
                    }
                } else {
                    $ar_result = $this->Transport->get_area(array('area_id'=> $area_id));
                    if(count($ar_result) == 0) {
                        $data['errors'] = array("Could not find the selected area");
                    } else {
                        foreach($ar_result[0] as $key => $area_item) {
                            $data[$key] = $area_item;
                        }
                    }
                }
            } else {
                $data['error_message'] = "Invalid request. Please contact the administrator.";
                $this->load->view('error_others',$data);
            }            
                                
            $navResult = $this->Dataset_db->buildNav();
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            //print_object($data); exit;
            $this->load->view('editarea',$data);
        }
        
        function arealist() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; 
            }
            $data['ptitle'] = "Area List";
            //print_object($_POST); exit;
            $filterdata = array(
                'items' => array(
                    array(
                        'label' => 'Area ID',
                        'table' => 'ar',
                        'name' => 'area_id',
                        'value' => isset( $_POST["area_id"] ) ? $_POST["area_id"] : '',
                        'type' => 'text',
                        'data' => ''
                    ),
                    array(
                        'label' => 'Area Name',
                        'table' => 'ar',
                        'name' => 'area_name',
                        'value' => isset( $_POST["area_name"] ) ? $_POST["area_name"] : '',
                        'type' => 'text',
                        'data' => ''
                    ),
                    array(
                        'label' => 'Area Code',
                        'table' => 'ar',
                        'name' => 'area_code',
                        'value' => isset( $_POST["area_code"] ) ? $_POST["area_code"] : '',
                        'type' => 'text',
                        'data' => ''
                    ),
                    array(
                        'label' => 'Transport Fee',
                        'table' => 'ar',
                        'name' => 'area_transport_fee',
                        'value' => isset( $_POST["area_transport_fee"] ) ? $_POST["area_transport_fee"] : '',
                        'type' => 'text',
                        'data' => ''
                    )
                ),
                'action' => '/transport/arealist',
                'submit' => 'btnSearchArea',
                'meta' => $this->Transport->get_area_meta(),
                'cols' => isset( $_POST["columns"] ) ? $_POST["columns"] : '',
            );
            
            if($this->input->post('show_all')) {
                $data['show_all'] = true;
            }
            
            $data['dgdata'] = $this->Transport->build_area_dg_data($filterdata);
            
            $data['filterdata'] = $filterdata;
               
            //print_object($data);exit;
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            //print_object($data); exit;
            $this->load->view('searchitem',$data);            
        }
        
        function routelist() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; 
            }
            $data['ptitle'] = "Route List";
            //print_object($_POST); exit;
            $filterdata = array(
                'items' => array(
                    array(
                        'label' => 'Route ID',
                        'table' => 'ru',
                        'name' => 'route_id',
                        'value' => isset( $_POST["route_id"] ) ? $_POST["route_id"] : '',
                        'type' => 'text',
                        'data' => ''
                    ),
                    array(
                        'label' => 'Route Name',
                        'table' => 'ru',
                        'name' => 'route_name',
                        'value' => isset( $_POST["route_name"] ) ? $_POST["route_name"] : '',
                        'type' => 'text',
                        'data' => ''
                    )
                ),
                'action' => '/transport/routelist',
                'submit' => 'btnSearchRoute',
                'meta' => $this->Transport->get_route_meta(),
                'cols' => isset( $_POST["columns"] ) ? $_POST["columns"] : '',
            );
            
            if($this->input->post('show_all')) {
                $data['show_all'] = true;
            }
            
            $data['dgdata'] = $this->Transport->build_route_dg_data($filterdata);
            
            $data['filterdata'] = $filterdata;
               
            //print_object($data);exit;
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            //print_object($data); exit;
            $this->load->view('searchitem',$data);            
        }
       
       
        function addroute() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; 
            }
            //print_object($_POST); exit;
            $data = array();

            if($_POST['btnAddRoute']) {
                //print_object($_POST); exit;
                $pdata = array();
                $fields = array(
                		"route_name"		
                		);
                foreach ($fields as $field) {
                	if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field, true);
                }
                
                $this->form_validation->set_rules('route_name', 'Route Name', 'trim|required|max_length[50]|xss_clean');
                
                $route_area = $_POST["route_area"];
                $this->form_validation->run();
                $data["errors"] = $this->form_validation->error_array();
                
                if( is_array($route_area) && count($route_area) > 0 ) {
                    
                } else {
                    if(is_array($data['errors']))
                        array_push($data["errors"],"Route area is required.");
                    else
                        $data["errors"] = array("Route area is required.");
                }
                
                if(!$this->Transport->is_routename_unique($this->input->post("route_name",true))) {
                    if(is_array($data['errors']))
                        array_push($data["errors"],"Route already exist.");
                    else
                        $data["errors"] = array("Route already exist.");
                }
                
                $data["postdata"] = $pdata;
                   
                if (count($data["errors"])>0) {
                    $data["postdata"]["route_area"] = $route_area;
                } else {
                    //print_object($_POST); exit;
                    $user = $this->session->userdata('user');
                    $pdata["createdby"] = $user['userid'];
                    $pdata["createdon"] = unix_to_human(app_time(), true);
                    
                    
                    $save_result = $this->Transport->save_route($pdata);
                    if($save_result['result'] == 'S') {
                        $route_id = $save_result["insert_id"];
                        $ra_result = $this->Transport->save_route_area($route_area,$route_id);
                        $data["successes"] = array('Route added successfully.');
                        $data["postdata"] = "";
                    } else {
                        $data["errors"] = array($save_result['error_message']); 
                        $data["postdata"]["route_area"] = $route_area;  
                    }
                    //print_object($data); exit;   
                }
            }
                                
            $navResult = $this->Dataset_db->buildNav();
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $data['arealist'] = $this->Transport->get_area_list();
            //print_object($data); exit;
            $this->load->view('addroute',$data);
        }
        
        function addvehiclestaff() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; 
            }
            //print_object($_POST); exit;
            $data = array();

            if($_POST['btnAddStaff']) {
                //print_object($_POST); exit;
                $pdata = array();
                $fields = array(
                		"vs_staff_type",
                        "vs_name",
                        "vs_joining_date",
                        "vs_dob",
                        "vs_email",
                        "vs_mobile",
                        "vs_phone",
                        "vs_address",
                        "vs_nationality",
                        "vs_rp_no",
                        "vs_rp_expiry",
                        "vs_sponsor_name",
                        "vs_license_no",
                        "vs_license_expiry"			
                		);
                foreach ($fields as $field) {
                	if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field, true);
                }
                
                $this->form_validation->set_rules('vs_name', 'Name', 'trim|required|min_length[3]|max_length[100]|xss_clean');
                $this->form_validation->set_rules('vs_staff_type', 'Staff Type', 'trim|required|xss_clean');
                $this->form_validation->set_rules('vs_joining_date', 'Joining Date', 'trim|required|xss_clean');
                $this->form_validation->set_rules('vs_dob', 'Date of birth', 'trim|required|xss_clean');
                $this->form_validation->set_rules('vs_email', 'Email', 'trim|valid_email|min_length[3]|max_length[60]|xss_clean');
                $this->form_validation->set_rules('vs_mobile', 'Mobile', 'trim|required|min_length[3]|max_length[15]|xss_clean');
                $this->form_validation->set_rules('vs_phone', 'Telephone', 'trim|min_length[3]|max_length[15]|xss_clean');
                $this->form_validation->set_rules('vs_address', 'Address', 'trim|required|min_length[3]|max_length[150]|xss_clean');
                $this->form_validation->set_rules('vs_nationality', 'Nationality', 'trim|required|xss_clean');
                $this->form_validation->set_rules('vs_rp_no', 'RP No.', 'trim|required|min_length[8]|max_length[20]|xss_clean');
                $this->form_validation->set_rules('vs_rp_expiry', 'RP Expiry', 'trim|required|xss_clean');
                $this->form_validation->set_rules('vs_sponsor_name', 'Sponsor Name', 'trim|min_length[3]|max_length[100]|xss_clean');
                $this->form_validation->set_rules('vs_license_no', 'License No.', 'trim|required|min_length[4]|max_length[20]|xss_clean');
                $this->form_validation->set_rules('vs_license_expiry', 'License Expiry', 'trim|required|xss_clean');
                                
                $this->form_validation->run();
                $data["errors"] = $this->form_validation->error_array();
                
                $data["postdata"] = $pdata;
                   
                if (count($data["errors"])>0) {
                    
                } else {
                    $user = $this->session->userdata('user');
                    $pdata["vs_createdby"] = $user['userid'];
                    $pdata["vs_createdon"] = unix_to_human(app_time(), true);
                    $pdata["vs_status"] = 1;    // New Registration.
                    //print_object($pdata); exit;
                    
                    $save_result = $this->Transport->save_staff($pdata);
                    if($save_result['result'] == 'S') {
                        $data["successes"] = array('Staff added successfully.');
                        $data["postdata"] = "";
                    } else {
                        $data["errors"] = array($save_result['error_message']);   
                    }   
                }
            }
                                
            $navResult = $this->Dataset_db->buildNav();
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $data['vstlist'] = format_list($this->Transport->get_vehiclestafftype(),'vst_id','vst_name');
            $data['countrylist'] = $this->Dataset_db->get_countries();
            $this->load->view('addvehiclestaff',$data);
        }
        
        function vehiclestafflist() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            $data['ptitle'] = "Bus Staff List";
            //print_object($_POST); exit;
            $filterdata = array(
                'items' => array(
                    array(
                        'label' => 'Staff ID',
                        'table' => 'vs',
                        'name' => 'vs_id',
                        'value' => isset( $_POST["vs_id"] ) ? $_POST["vs_id"] : '',
                        'type' => 'text',
                        'data' => ''
                    ),
                    array(
                        'label' => 'Name',
                        'table' => 'vs',
                        'name' => 'vs_name',
                        'value' => isset( $_POST["vs_name"] ) ? $_POST["vs_name"] : '',
                        'type' => 'text',
                        'data' => ''
                    ),
                    array(
                        'label' => 'Staff Type',
                        'table' => 'vs',
                        'name' => 'vs_staff_type',
                        'value' => isset( $_POST["vs_staff_type"] ) ? $_POST["vs_staff_type"] : '',
                        'type' => 'select',
                        'data' => format_list($this->Transport->get_vehiclestafftype(),'vst_id','vst_name')
                    ),
                    array(
                        'label' => 'Status',
                        'table' => 'vs',
                        'name' => 'vs_status',
                        'value' => isset( $_POST["vs_status"] ) ? $_POST["vs_status"] : '',
                        'type' => 'select',
                        'data' => format_list($this->Transport->get_staffstatus(),'ss_id','ss_name')
                    )
                ),
                'action' => '/transport/vehiclestafflist',
                'submit' => 'btnSearchStaff',
                'meta' => $this->Transport->get_staff_meta(),
                'cols' => isset( $_POST["columns"] ) ? $_POST["columns"] : '',
            );
            
            if($this->input->post('show_all')) {
                $data['show_all'] = true;
            }
            
            $data['dgdata'] = $this->Transport->build_vs_dg_data($filterdata);
            
            $data['filterdata'] = $filterdata;
               
            //print_object($data);exit;
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            //print_object($data); exit;
            $this->load->view('searchitem',$data);
        }
        
        function viewvehiclestaff($staffid) {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            $data = array();
            if($staffid!="") {
                $staffid = xss_clean($staffid);
                $vs_result = $this->Transport->get_vehiclestaff(array('vs_id'=> $staffid));
                if(count($vs_result) == 0) {
                    $data['errors'] = "Could not find the selected staff";
                } else {
                    foreach($vs_result[0] as $key => $vs_item) {
                        $data[$key] = $vs_item;
                    }
                }
                
                $navResult = $this->Dataset_db->buildNav();
                $data['navs'] = $navResult['links'];
                $data['clabel'] = $navResult['selected']['group'];            
                $data['mlabel'] = $navResult['selected']['item'];
                //print_object($data); exit;
            } else {
                $data['errors'] = "Invalid request!";
            }
            $this->load->view('vstaffdetails',$data);    
        }
        
        function viewvehicle($vehicleid) {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            $data = array();
            if($vehicleid!="") {
                $vehicleid = xss_clean($vehicleid);
                $vs_result = $this->Transport->get_vehicle(array('vh_id'=> $vehicleid));
                if(count($vs_result) == 0) {
                    $data['errors'] = "Could not find the selected vehicle";
                } else {
                    foreach($vs_result[0] as $key => $vs_item) {
                        $data[$key] = $vs_item;
                    }
                }
                
                $navResult = $this->Dataset_db->buildNav();
                $data['navs'] = $navResult['links'];
                $data['clabel'] = $navResult['selected']['group'];            
                $data['mlabel'] = $navResult['selected']['item'];
            } else {
                $data['errors'] = "Invalid request!";
            }
            //print_object($data); exit;
            $this->load->view('vehicledetails',$data);    
        }
        
        function editvehiclestaff() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; 
            }
            //print_object($_POST); exit;
            $data = array();
            
            $vs_id = $this->input->post("vs_id", true);
            
            if( isset( $vs_id ) ) {
                if($_POST['btnUpdateStaff']) {
                    //print_object($_POST); exit;
                    $pdata = array();
                    $fields = array(
                    		"vs_staff_type",
                            "vs_name",
                            "vs_joining_date",
                            "vs_dob",
                            "vs_email",
                            "vs_mobile",
                            "vs_phone",
                            "vs_address",
                            "vs_nationality",
                            "vs_rp_no",
                            "vs_rp_expiry",
                            "vs_sponsor_name",
                            "vs_license_no",
                            "vs_license_expiry",
                            "vs_status",
                            "vs_leaving_date",
                            "vs_leaving_reason"			
                    		);                    
                    
                    $this->form_validation->set_rules('vs_name', 'Name', 'trim|required|min_length[3]|max_length[100]|xss_clean');
                    $this->form_validation->set_rules('vs_staff_type', 'Staff Type', 'trim|required|xss_clean');
                    $this->form_validation->set_rules('vs_joining_date', 'Joining Date', 'trim|required|xss_clean');
                    $this->form_validation->set_rules('vs_dob', 'Date of birth', 'trim|required|xss_clean');
                    $this->form_validation->set_rules('vs_email', 'Email', 'trim|required|valid_email|min_length[3]|max_length[60]|xss_clean');
                    $this->form_validation->set_rules('vs_mobile', 'Mobile', 'trim|required|min_length[3]|max_length[15]|xss_clean');
                    $this->form_validation->set_rules('vs_phone', 'Telephone', 'trim|min_length[3]|max_length[15]|xss_clean');
                    $this->form_validation->set_rules('vs_address', 'Address', 'trim|required|min_length[3]|max_length[150]|xss_clean');
                    $this->form_validation->set_rules('vs_nationality', 'Nationality', 'trim|required|xss_clean');
                    $this->form_validation->set_rules('vs_rp_no', 'RP No.', 'trim|required|min_length[8]|max_length[20]|xss_clean');
                    $this->form_validation->set_rules('vs_rp_expiry', 'RP Expiry', 'trim|required|xss_clean');
                    $this->form_validation->set_rules('vs_sponsor_name', 'Sponsor Name', 'trim|min_length[3]|max_length[100]|xss_clean');
                    $this->form_validation->set_rules('vs_license_no', 'License No.', 'trim|required|min_length[4]|max_length[20]|xss_clean');
                    $this->form_validation->set_rules('vs_license_expiry', 'License Expiry', 'trim|required|xss_clean');
                    $this->form_validation->set_rules('vs_status', 'Status', 'trim|required|xss_clean');
                    $this->form_validation->set_rules('vs_leaving_date', 'Leaving Date', 'trim|xss_clean');
                    $this->form_validation->set_rules('vs_leaving_reason', 'Reason for Leaving', 'trim|min_length[3]|max_length[250]|xss_clean');
                            
                    foreach ($fields as $field) {
                    	if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field, true);
                    }
                                    
                    $this->form_validation->run();
                    $data["errors"] = $this->form_validation->error_array();
                    
                    $data["postdata"] = $pdata;
                       
                    if (count($data["errors"])>0) {
                        
                    } else {
                        $user = $this->session->userdata('user');
                        $pdata["vs_createdby"] = $user['userid'];
                        $pdata["vs_createdon"] = unix_to_human(app_time(), true);

                        //print_object($pdata); exit;
                        
                        $save_result = $this->Transport->save_staff($pdata, $vs_id);
                        if($save_result['result'] == 'S') {
                            $data["successes"] = array('Staff updated successfully.');
                            $data["postdata"] = "";
                            $vs_result = $this->Transport->get_vehiclestaff(array('vs_id'=> $vs_id));
                            if(count($vs_result) == 0) {
                                $data['errors'] = array("Could not find the selected staff");
                            } else {
                                foreach($vs_result[0] as $key => $vs_item) {
                                    $data[$key] = $vs_item;
                                }
                            }
                        } else {
                            $data["errors"] = array($save_result['error_message']);  
                            foreach($pdata as $key => $st_item) {
                                $data[$key] = $st_item;
                            } 
                        }   
                    }
                } else {
                    $vs_result = $this->Transport->get_vehiclestaff(array('vs_id'=> $vs_id));
                    if(count($vs_result) == 0) {
                        $data['errors'] = array("Could not find the selected staff");
                    } else {
                        foreach($vs_result[0] as $key => $vs_item) {
                            $data[$key] = $vs_item;
                        }
                    }
                }
            } else {
                $data['error_message'] = "Invalid request. Please contact the administrator.";
                $this->load->view('error_others',$data);
            }            
                                
            $navResult = $this->Dataset_db->buildNav();
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $data['vstlist'] = format_list($this->Transport->get_vehiclestafftype(),'vst_id','vst_name');
            $data['countrylist'] = $this->Dataset_db->get_countries();
            $data['vsslist'] = format_list($this->Transport->get_staffstatus(),'ss_id','ss_name');
            //print_object($data); exit;
            $this->load->view('editvehiclestaff',$data);
        }
        
        function viewarea($areaid) {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            $data = array();
            if($areaid!="") {
                $areaid = xss_clean($areaid);
                $ar_result = $this->Transport->get_area(array('area_id'=> $areaid));
                if(count($ar_result) == 0) {
                    $data['errors'] = "Could not find the selected area.";
                } else {
                    foreach($ar_result[0] as $key => $ar_item) {
                        $data[$key] = $ar_item;
                    }
                }
                
                $navResult = $this->Dataset_db->buildNav();
                $data['navs'] = $navResult['links'];
                $data['clabel'] = $navResult['selected']['group'];            
                $data['mlabel'] = $navResult['selected']['item'];
                //print_object($data); exit;
            } else {
                $data['errors'] = "Invalid request!";
            }
            $this->load->view('areadetails',$data);    
        }
        
        function viewroute($routeid) {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            $data = array();
            if($routeid!="") {
                $routeid = xss_clean($routeid);
                $ru_result = $this->Transport->get_route(array('route_id'=> $routeid));
                if(count($ru_result) == 0) {
                    $data['errors'] = "Could not find the selected route.";
                } else {
                    foreach($ru_result[0] as $key => $ru_item) {
                        $data[$key] = $ru_item;
                    }
                }
                
                $navResult = $this->Dataset_db->buildNav();
                $data['navs'] = $navResult['links'];
                $data['clabel'] = $navResult['selected']['group'];            
                $data['mlabel'] = $navResult['selected']['item'];
                $data['arealist'] = $this->Transport->get_area_list();
                //print_object($data); exit;
            } else {
                $data['errors'] = "Invalid request!";
            }
            $this->load->view('routedetails',$data);    
        }
        
        
        function editroute() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; 
            }
            //print_object($_POST); exit;
            $data = array();
            
            $route_id = $this->input->post("route_id", true);
            
            if( isset( $route_id ) ) {
                if($_POST['btnUpdateRoute']) {
                    //print_object($_POST); exit;
                    $pdata = array();
                    $fields = array(
                    		"route_name"		
                    		);
                    foreach ($fields as $field) {
                    	if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field, true);
                    }
                    
                    $this->form_validation->set_rules('route_name', 'Route Name', 'trim|required|max_length[50]|xss_clean');
                    
                    $route_area = $_POST["route_area"];
                    $this->form_validation->run();
                    $data["errors"] = $this->form_validation->error_array();
                    
                    if( is_array($route_area) && count($route_area) > 0 ) {
                        
                    } else {
                        if(is_array($data['errors']))
                            array_push($data["errors"],"Route area is required.");
                        else
                            $data["errors"] = array("Route area is required.");
                    }
                    
                    if(!$this->Transport->is_routename_unique($this->input->post("route_name",true),$route_id)) {
                        if(is_array($data['errors']))
                            array_push($data["errors"],"Route already exist.");
                        else
                            $data["errors"] = array("Route already exist.");
                    }
                    
                    $data["postdata"] = $pdata;
                    
                    if (count($data["errors"])>0) {
                        $data["route_id"] = $route_id;
                        $data["route_area"] = $route_area;  
                        foreach($pdata as $key => $route_item) {
                            $data[$key] = $route_item;
                        }
                    } else {
                        $save_result = $this->Transport->save_route($pdata,$route_id);
                        if($save_result['result'] == 'S') {
                            $ra_result = $this->Transport->save_route_area($route_area,$route_id);
                            $data["successes"] = array('Route updated successfully.');
                            $data["postdata"] = "";
                            $ru_result = $this->Transport->get_route(array('route_id'=> $route_id));
                            if(count($ru_result) == 0) {
                                $data['errors'] = array("Could not find the selected route.");
                            } else {
                                foreach($ru_result[0] as $key => $route_item) {
                                    $data[$key] = $route_item;
                                }
                            }
                        } else {
                            $data["errors"] = array($save_result['error_message']); 
                            $data["route_area"] = $route_area;  
                            $data["route_id"] = $route_id;
                            foreach($pdata as $key => $route_item) {
                                $data[$key] = $route_item;
                            }
                        }
                        //print_object($data); exit;   
                    }
                } else {
                    $ru_result = $this->Transport->get_route(array('route_id'=> $route_id));
                    if(count($ru_result) == 0) {
                        $data['errors'] = array("Could not find the selected area");
                    } else {
                        foreach($ru_result[0] as $key => $route_item) {
                            $data[$key] = $route_item;
                        }
                    }
                }
                $data['arealist'] = $this->Transport->get_area_list();
            } else {
                $data['error_message'] = "Invalid request. Please contact the administrator.";
                $this->load->view('error_others',$data);
            }            
                                
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            //print_object($data); exit;
            $this->load->view('editroute',$data);
        }
        
        function editvehicle() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; 
            }
            //print_object($_POST); exit;
            $data = array();
            
            $vh_id = $this->input->post("vh_id", true);
            
            if( isset( $vh_id ) ) {
                if($_POST['btnUpdateVehicle']) {
                    //print_object($_POST); exit;
                    $pdata = array();
                    $fields = array(
                		"vh_no",
                        "vh_make",
                        "vh_model",
                        "vh_capacity",
                        "vh_reg_no",
                        "vh_reg_expiry",
                        "vh_first_reg_date",
                        "vh_owner",
                        "vh_owner_tel",
                        "vh_route",
                        "vh_driver",
                        "vh_helper",
                        "vh_status"			
              		);                    
                    
                    $this->form_validation->set_rules('vh_no', 'Bus No.', 'trim|required|min_length[1]|max_length[10]|xss_clean');
                    $this->form_validation->set_rules('vh_make', 'Make', 'trim|required|xss_clean');
                    $this->form_validation->set_rules('vh_model', 'Model', 'trim|required|xss_clean');
                    $this->form_validation->set_rules('vh_capacity', 'Capacity', 'trim|required|integer|xss_clean');
                    $this->form_validation->set_rules('vh_reg_expiry', 'Registration Expiry', 'trim|required|xss_clean');
                    $this->form_validation->set_rules('vh_first_reg_date', 'Date of First Regn', 'trim|required|xss_clean');
                            
                    foreach ($fields as $field) {
                    	if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field, true);
                    }
                                    
                    $this->form_validation->run();
                    $data["errors"] = $this->form_validation->error_array();
                    
                    $data["postdata"] = $pdata;
                       
                    if (count($data["errors"])>0) {
                        
                    } else {                        
                        $save_result = $this->Transport->save_vehicle($pdata, $vh_id);
                        if($save_result['result'] == 'S') {
                            $data["successes"] = array('Staff updated successfully.');
                            $data["postdata"] = "";
                            $vh_result = $this->Transport->get_vehicle(array('vh_id'=> $vh_id));
                            if(count($vh_result) == 0) {
                                $data['errors'] = array("Could not find the selected staff");
                            } else {
                                foreach($vh_result[0] as $key => $vh_item) {
                                    $data[$key] = $vh_item;
                                }
                            }
                        } else {
                            $data["errors"] = array($save_result['error_message']);  
                            foreach($pdata as $key => $st_item) {
                                $data[$key] = $st_item;
                            } 
                        }   
                    }
                } else {
                    $vh_result = $this->Transport->get_vehicle(array('vh_id'=> $vh_id));
                    if(count($vh_result) == 0) {
                        $data['errors'] = array("Could not find the selected vehicle");
                    } else {
                        foreach($vh_result[0] as $key => $vh_item) {
                            $data[$key] = $vh_item;
                        }
                    }
                }
            } else {
                $data['error_message'] = "Invalid request. Please contact the administrator.";
                $this->load->view('error_others',$data);
            }            
                                
            $navResult = $this->Dataset_db->buildNav();
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            
            $data['vsslist'] = format_list($this->Transport->get_staffstatus(),'ss_id','ss_name');
            $data['driverlist'] = format_list(
                $this->Transport->get_vehiclestaff(array(
                    'vs_staff_type'=>1, //Driver
                    'vs_status'=>2  //Select only active drivers
                )),
                'vs_id',
                'vs_name'
            );
            $data['helperlist'] = format_list(
                $this->Transport->get_vehiclestaff(array(
                    'vs_staff_type'=>2, //Helper
                    'vs_status'=>2  //Select only active helpers
                )),
                'vs_id',
                'vs_name'
            );
            $data['routelist'] = format_list(
                $this->Transport->get_routes(),
                'route_id',
                'route_name'
            );
            //print_object($data); exit;
            $this->load->view('editvehicle',$data);
        }
        
        function addvehicle() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; 
            }
            
            //print_object($_POST); exit;
            $data = array();

            if($_POST['btnAddVehicle']) {
                //print_object($_POST); exit;
                $pdata = array();
                $fields = array(
                		"vh_no",
                        "vh_make",
                        "vh_model",
                        "vh_capacity",
                        "vh_reg_no",
                        "vh_reg_expiry",
                        "vh_first_reg_date",
                        "vh_owner",
                        "vh_owner_tel",
                        "vh_route",
                        "vh_driver",
                        "vh_helper"			
              		);
                foreach ($fields as $field) {
                	if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field, true);
                }
                
                $this->form_validation->set_rules('vh_no', 'Bus No.', 'trim|required|min_length[1]|max_length[10]|xss_clean');
                $this->form_validation->set_rules('vh_make', 'Make', 'trim|required|xss_clean');
                $this->form_validation->set_rules('vh_model', 'Model', 'trim|required|xss_clean');
                $this->form_validation->set_rules('vh_capacity', 'Capacity', 'trim|required|integer|xss_clean');
                $this->form_validation->set_rules('vh_reg_expiry', 'Registration Expiry', 'trim|required|xss_clean');
                $this->form_validation->set_rules('vh_first_reg_date', 'Date of First Regn', 'trim|required|xss_clean');
                                                
                $this->form_validation->run();
                $data["errors"] = $this->form_validation->error_array();
                
                $data["postdata"] = $pdata;
                   
                if (count($data["errors"])>0) {
                    
                } else {
                    $user = $this->session->userdata('user');
                    $pdata["vh_createdby"] = $user['userid'];
                    $pdata["vh_createdon"] = unix_to_human(app_time(), true);
                    $pdata["vh_status"] = 1;    // New Registration.
                    //print_object($pdata); exit;
                    
                    $save_result = $this->Transport->save_vehicle($pdata);
                    if($save_result['result'] == 'S') {
                        $data["successes"] = array('Vehicle added successfully.');
                        $data["postdata"] = "";
                    } else {
                        $data["errors"] = array($save_result['error_message']);   
                    }   
                }
            }

            $navResult = $this->Dataset_db->buildNav();
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $data['driverlist'] = format_list(
                $this->Transport->get_vehiclestaff(array(
                    'vs_staff_type'=>1, //Driver
                    'vs_status'=>2  //Select only active drivers
                )),
                'vs_id',
                'vs_name'
            );
            $data['helperlist'] = format_list(
                $this->Transport->get_vehiclestaff(array(
                    'vs_staff_type'=>2, //Helper
                    'vs_status'=>2  //Select only active helpers
                )),
                'vs_id',
                'vs_name'
            );
            $data['routelist'] = format_list(
                $this->Transport->get_routes(),
                'route_id',
                'route_name'
            );
            //print_object($data); exit;
            $this->load->view('addvehicle',$data);
        }
        
        function vehiclelist() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            $data['ptitle'] = "Vehicle List";
            //print_object($_POST); exit;
            $filterdata = array(
                'items' => array(
                    array(
                        'label' => 'Bus No.',
                        'table' => 'vh',
                        'name' => 'vh_no',
                        'value' => isset( $_POST["vh_no"] ) ? $_POST["vh_no"] : '',
                        'type' => 'text',
                        'data' => ''
                    ),
                    array(
                        'label' => 'Model',
                        'table' => 'vh',
                        'name' => 'vh_model',
                        'value' => isset( $_POST["vh_model"] ) ? $_POST["vh_model"] : '',
                        'type' => 'text',
                        'data' => ''
                    ),
                    array(
                        'label' => 'Registration No.',
                        'table' => 'vh',
                        'name' => 'vh_reg_no',
                        'value' => isset( $_POST["vh_reg_no"] ) ? $_POST["vh_reg_no"] : '',
                        'type' => 'text',
                        'data' => ''
                    ),
                    array(
                        'label' => 'Route',
                        'table' => 'vh',
                        'name' => 'vh_route',
                        'value' => isset( $_POST["vh_route"] ) ? $_POST["vh_route"] : '',
                        'type' => 'select',
                        'data' => format_list($this->Transport->get_routes(),'route_id','route_name')
                    ),
                    array(
                        'label' => 'Status',
                        'table' => 'vh',
                        'name' => 'vh_status',
                        'value' => isset( $_POST["vh_status"] ) ? $_POST["vh_status"] : '',
                        'type' => 'select',
                        'data' => format_list($this->Transport->get_staffstatus(),'ss_id','ss_name')
                    )
                ),
                'action' => '/transport/vehiclelist',
                'submit' => 'btnSearchVehicle',
                'meta' => $this->Transport->get_vehicle_meta(),
                'cols' => isset( $_POST["columns"] ) ? $_POST["columns"] : '',
            );
            
            if($this->input->post('show_all')) {
                $data['show_all'] = true;
            }
            
            $data['dgdata'] = $this->Transport->build_vh_dg_data($filterdata);
            
            $data['filterdata'] = $filterdata;
               
            //print_object($data);exit;
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            //print_object($data); exit;
            $this->load->view('searchitem',$data);
        }
        
    }       
/* End of file */